package com.aliyun.sms.demo;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsResponse;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

import java.text.SimpleDateFormat;
import java.util.Date;

public class aliyun_sms {

    //产品名称:云通信短信API产品,开发者无需替换
    static final String product = "Dysmsapi";
    //产品域名,开发者无需替换
    static final String domain = "dysmsapi.aliyuncs.com";

    // TODO 此处需要替换成开发者自己的AK(在阿里云访问控制台寻找)
    static final String accessKeyId = "LTAIvnLs0XBWM0k8";
    static final String accessKeySecret = "aDtIg8ytcwLn4nGylWfKLykYVt5t5x";

//    发送服务
    public static SendSmsResponse sendSms() throws ClientException {

        //可自助调整超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");

        //初始化acsClient,暂不支持region化
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        IAcsClient acsClient = new DefaultAcsClient(profile);

        //组装请求对象-具体描述见控制台-文档部分内容
        SendSmsRequest request = new SendSmsRequest();
        //必填:待发送手机号

//测试用
//        request.setPhoneNumbers("13546323367");
//正式

//        request.setPhoneNumbers("18310081396,"+
//                "13837156890,"+
//                        "15038255555,"+
//                        "18571485536,"+
//                        "18571570891,"+
//                        "15317812027,"+
//                        "13811300048,"+
//                        "13801352351,"+
//                        "13555521111,"+
//                        "13818908188,"+
//                        "18698569217,"+
//                        "18583773636,"+
//                        "13712213132,"+
//                        "15928620740,"+
//                        "18906431717,"+
//                        "15920765743,"+
//                        "13911470037,"+
//                        "18612612501,"+
//                        "13430329444,"+
//                        "18611470768,"+
//                        "13811918670,"+
//                        "15112672277,"+
//                        "13713507337,"+
//                        "13301515175,"+
//                        "18811998100,"+
//                        "13611841386,"+
//                        "13755117062,"+
//                        "13864399170,"+
//                        "13801055037,"+
//                        "18600044892,"+
//                        "18867513636,"+
//                        "18201931325,"+
//                        "18811759122,"+
//                        "13585868572,"+
//                        "18616305622,"+
//                        "13758117211,"+
//                        "18611203907,"+
//                        "15859085976,"+
//                        "18616108796,"+
//                        "15901043986,"+
//                        "13851578991,"+
//                        "18600714201,"+
//                        "13942815506,"+
//                        "13802514032,"+
//                        "18925071758,"+
//                        "18612600918,"+
//                        "18612722903,"+
//                        "13611177757,"+
//                        "13643713292,"+
//                        "13607658086,"+
//                        "18620043208,"+
//                        "13761378036,"+
//                        "15618825309,"+
//                        "15000564820,"+
//                        "13823522866,"+
//                        "18561817320,"+
//                        "13791938808,"+
//                        "18128860955,"+
//                        "17621154726,"+
//                        "18561533239,"+
//                        "18669823882,"+
//                        "18813150763,"+
//                        "18753212163,"+
//                        "15120005189,"+
//                        "13522883879,"+
//                        "13983665169,"+
//                        "18623663577,"+
//                        "13193195221,"+
//                        "13693172609,"+
//                        "13510091964,"+
//                        "18321171882,"+
//                        "18696594773,"+
//                        "18962555118,"+
//                        "15118008333,"+
//                        "18601133262,"+
//                        "13802525816,"+
//                        "13611018356,"+
//                        "18611876005,"+
//                        "18611876005,"+
//                        "18611876005,"+
//                        "18208111186,"+
//                        "13902948418,"+
//                        "13501368128,"+
//                        "18910700595,"+
//                        "13881992200,"+
//                        "13551395357,"+
//                        "18601158415,"+
//                        "18701593002,"+
//                        "13810215185,"+
//                        "13905312917,"+
//                        "15005668837,"+
//                        "18156069160,"+
//                        "13817930535,"+
//                        "15618575845,"+
//                        "15922033875,"+
//                        "13752133626,"+
//                        "18511896525,"+
//                        "15966063990,"+
//                        "18905318955,"+
//                        "18616349647,"+
//                        "13621971786,"+
//                        "15121045582,"+
//                        "13308362729,"+
//                        "13524860811,"+
//                        "13984153380,"+
//                        "18610125558,");

//        已发送
        request.setPhoneNumbers(
                        "15666209316,"+
                        "13011201030,"+
                        "13681779911,"+
                        "13681210400,"+
                        "13910322049,"+
                        "15804258707,"+
                        "18850070101,"+
                        "13601115732,"+
                        "13898890741,"+
                        "18910377260,"+
                        "13901245169,"+
                        "13808061217,"+
                        "18902302619,"+
                        "15601010202,"+
                        "15000910785,"+
                        "18962185576,"+
                        "18962185576,"+
                        "18962185576,"+
                        "18962185576,"+
                        "18962185576,"+
                        "15356891899,"+
                        "13510890626,"+
                        "18856077812,"+
                        "13510890626,"+
                        "18620021736,"+
                        "13302269373,"+
                        "18513830926,"+
                        "13661979384,"+
                        "18038853955,"+
                        "18038853955,"+
                        "18038853955,"+
                        "18038853955,"+
                        "18964726601,"+
                        "13595034134,"+
                        "13285107755,"+
                        "13285107755,"+
                        "13682525214,"+
                        "14716031247,"+
                        "13502870506,"+
                        "13600066908,"+
                        "13527769570,"+
                        "13916699791,"+
                        "13566777391,"+
                        "13917097955,"+
                        "13764072186,"+
                        "13735674344,"+
                        "13699225875,"+
                        "15138958946,"+
                        "18570602335,"+
                        "18013893335,"+
                        "18108234698,"+
                        "15811083129,"+
                        "13636575136,"+
                        "13924198826,"+
                        "15800031636,"+
                        "13842443883,"+
                        "13566772237,"+
                        "15208338820,"+
                        "18922390136,"+
                        "18643078077,"+
                        "13510890626,"+
                        "13777788080,"+
                        "15017550834,"+
                        "13326475286,"+
                        "13902388384,"+
                        "18601257038,"+
                        "15261854193,"+
                        "13524616840,"+
                        "18606313102,"+
                        "15606307110,"+
                        "18680212076,"+
                        "15010232007,"+
                        "13711065117,"+
                        "13431002292,"+
                        "13903024468,"+
                        "18011875668,"+
                        "18311423458,"+
                        "15050450479,"+
                        "18221616961,"+
                        "18612255706,"+
                        "13758117211,"+
                        "18603428988,"+
                        "18583773636,"+
                        "13520960667,"+
                        "13855988897,"+
                        "18621070979,"+
                        "18520771912,"+
                        "13824922220,"+
                        "13881198336,"+
                        "13693172609,"+
                        "13880816780,"+
                        "18820033345,"+
                        "15821024789,"+
                        "13602807836,"+
                        "13902222079,"+
                        "18857521095,"+
                        "13816491094,"+
                        "13601782816,"+
                        "18610125558,"+
                        "13427641067,"+
                        "13143751014,"+
                        "18616389658,"+
                        "13576088018,"+
                        "15689691609,"+
                        "13910133507,"+
                        "13018931252,"+
                        "15021055305,"+
                        "13996418789,"+
                        "13983098633,"+
                        "15223124038,"+
                        "13811118933,"+
                        "15038255555,"+
                        "13837156890,"+
                        "18565618484,"+
                        "15021687169,"+
                        "18601158415,"+
                        "13810769567,"+
                        "13524365507,"+
                        "15317812027,"+
                        "13817559161,"+
                        "15161681900,"+
                        "13910531606,"+
                        "13717945188,"+
                        "18676576868,"+
                        "18801012833,"+
                        "15976991088,"+
                        "13311651055,"+
                        "13923836280,"+
                        "18602030253,"+
                        "18665370514,"+
                        "13916565433,"+
                        "15161667127,"+
                        "13143858520,"+
                        "13902388384,"+
                        "13261717446,"+
                        "18926086984,"+
                        "13894805999,"+
                        "13928569165,"+
                        "18503558586,"+
                        "18620043208,"+
                        "18682288628,"+
                        "13160980502,"+
                        "13501368128,"+
                        "13942815506,"+
                        "18620259909,"+
                        "17301209711,"+
                        "15652381779,"+
                        "13311618680,"+
                        "18571570891,"+
                        "18621651770,"+
                        "18602897765,"+
                        "18676638342,"+
                        "18571485536,"+
                        "13802514032,"+
                        "13825002253,"+
                        "18620259909,"+
                        "13911880688,"+
                        "18603428988,"+
                        "18653202045,"+
                        "18698569217,"+
                        "13612517236,"+
                        "18582053667,"+
                        "15626583306,"+
                        "13510404999,"+
                        "13922191689,"+
                        "18701097265,"+
                        "13585868572,"+
                        "15010324003,"+
                        "15611476946,"+
                        "18801735169,"+
                        "13611756569,"+
                        "13581807530,"+
                        "17765986301,"+
                        "18615769922,"+
                        "13764473606,"+
                        "15665768602,"+
                        "15901043986,"+
                        "15385547812,"+

                        "18810258376,"+
                        "13566057629,"+
                        "18600714201,"+
                        "13761378036,"+
                        "15618825309,"+
                        "15000564820,"+
                        "15120005189,"+
                        "18753212163,"+
                        "18669823882,"+
                        "18561533239,"+
                        "18561817320,"+
                        "13791938808,"+
                        "15800031636,"+
                        "14716031247,"+
                        "13682525214,"+
                        "13958070697,"+
                        "13958070697,"+
                        "13916699791,"+
                        "18601158415,"+
                        "18701593002,"+
                        "13810215185,"+
                        "13810215185,"+
                        "18621070979,"+
                        "18520771912,"+
                        "18962185576,"+
                        "18962185576,"+
                        "18962185576,"+
                        "18962185576,"+
                        "18962185576,"+
                        "13285107755,"+
                        "13285107755,"+
                        "18038853955,"+
                        "18038853955,"+
                        "18038853955,"+
                        "18038853955,"+
                        "13917097955,"+
                        "13764072186,"+
                        "18620302701,"+
                        "18807996699,"+
                        "18026282703,"+
                        "13923791199,"+
                        "18672960227,"+
                        "13811355456,"+
                        "13798319838,"+
                        "18818426357,"+
                        "13531836501,"+
                        "13008886631,"+
                        "13714712277,"+
                        "13923231716,"+
                        "13602807836,"+
                        "13622235754,"+
                        "18835569515,"+
                        "13631354487,"+
                        "13829278956,"+
                        "18613113006,"+
                        "13590286532,"+
                        "13714692216,"+
                        "13826079606,"+
                        "13501165728,"+
                        "18665389690,"+
                        "13922310135,"+
                        "13827447119,"+
                        "18811063186,"+
                        "15801566580,"+
                        "13802730982,"+
                        "13826215810,"+
                        "15603018992,"+
                        "18688858709,"+
                        "18611997592,"+
                        "15626090386,"+
                        "18520577989,"+
                        "13924803888,"+
                        "18928954552,"+
                        "15017542510,"+
                        "13880707241,"+
                        "13902948418,"+
                        "13924587677,"+
                        "13282684320,"+
                        "13825059090,"+
                        "13910702363,"+
                        "13510553841,"+
                        "13923763262,"+
                        "13923770080,"+
                        "18601270290,"+
                        "13699705342,"+
                        "13691935305,"+
                        "13751855055,"+
                        "13612937536,"+
                        "15521128075,"+
                        "13825250789,"+
                        "18022900340,"+
                        "18922018213,"+
                        "13798491011,"+
                        "13600186487,"+
                        "18620355629,"+
                        "13922745780,"+
                        "15914926030,"+
                        "18070081699,"+
                        "13823585886,"+
                        "18688665811,"+
                        "18588439817,"+
                        "15813836810,"+
                        "13712213132,"+
                        "13318960383,"+
                        "15820418437,"+
                        "13923468696,"+
                        "18688966149,"+
                        "13560762234,"+
                        "13798579430,"+
                        "13928091216,"+
                        "18610108228,"+
                        "13714519701,"+
                        "13713566022,"+
                        "18825049670,"+
                        "18929420773,"+
                        "13711201769,"+
                        "18502084087,"+
                        "13480934835,"+
                        "15814686656,"+
                        "18675555505,"+
                        "13902475850,"+
                        "15989474736,"+
                        "15012611599,"+
                        "13600005616,"+
                        "13723764855,"+
                        "13603090784,"+
                        "15017550834,"+
                        "13928757723,"+
                        "18718561131,"+
                        "13265302930,"+
                        "13923770320,"+
                        "18611209750,"+
                        "18620259903,"+
                        "13602646116,"+
                        "18513393421,"+
                        "13510107843,"+
                        "15820459935,"+
                        "13430393389,"+
                        "13510841712,"+
                        "15920680206,"+
                        "18998905599,"+
                        "18620259909,"+
                        "13922248101,"+
                        "15989005090,"+
                        "13822231086,"+
                        "13560727456,"+
                        "18824674959,"+
                        "13682525214,"+
                        "18927773865,"+
                        "18665329990,"+
                        "15989471285,"+
                        "15555199363,"+
                        "17704051623,"+
                        "18826487732,"+
                        "18620218953,"+
                        "18603097206,"+
                        "13922221539,"+
                        "15602229162,"+
                        "13801197278,"+
                        "13688808500,"+
                        "18122013383,"+
                        "13632313032,"+
                        "13620061288,"+
                        "13622258759,"+
                        "15007552699,"+
                        "13527274151,"+
                        "13823603306,"+
                        "13927468913,"+
                        "17896438062,"+
                        "13823619296,"+
                        "15989238951,"+
                        "13828497303,"+
                        "13602527920,"+
                        "13911461246,"+
                        "15807550388,"+
                        "18516963721,"+
                        "13817457288,"+
                        "13424166913,"+
                        "13340222560,"+
                        "13501588628,"+
                        "18610176445,"+
                        "13802642888,"+
                        "18988776841,"+
                        "18011875668,"+
                        "13003234399,"+
                        "18938048688,"+
                        "13510898707,"+
                        "13823306688,"+
                        "13902929297,"+
                        "13924185587,"+
                        "15960386556,"+
                        "15112672277,"+
                        "18665567620,"+
                        "13713507337,"+
                        "13902222079,"+
                        "13510091964,"+
                        "13926586090,"+
                        "18665323116,"+
                        "18520860612,"+
                        "15120005189,"+
                        "18128860955,"+
                        "15920667973,"+
                        "15976854474,"+
                        "18620043208,"+
                        "13616050043,"+
                        "18038116999,"+
                        "13030846250,"+
                        "18217526903,"+
                        "18680391430,"+
                        "13316040392,"+
                        "13458667968,"+
                        "18600530044,"+
                        "13719010905,"+
                        "13790223998,"+
                        "13632346632,"+
                        "18121321336,"+
                        "18902295986,"+
                        "13822744698,"+
                        "18011983513,"+
                        "13560308198,"+
                        "18688780296,"+
                        "18600849567,"+
                        "13760601235,"+
                        "13632366844,"+
                        "13603040228,"+
                        "15810067791,"+
                        "13823375072,"+
                        "13560790980,"+
                        "13302230223,"+
                        "13509622589,"+
                        "13763397323,"+
                        "13719282155,"+
                        "18902302619,"+
                        "18927536633,"+
                        "13808862260,"+
                        "13922304967,"+
                        "13560363200,"+
                        "18618415248,"+
                        "13902382010,"+
                        "18513153550,"+
                        "18602020118,"+
                        "18902292226,"+
                        "18520505624,"+
                        "13631538770,"+
                        "13760605852,"+
                        "13450374098,"+
                        "15012510906,"+
                        "13612856577,"+
                        "18922816113,"+
                        "13823047304,"+
                        "13823270212,"+
                        "15101112472,"+
                        "13902956545,"+
                        "13311117551,"+
                        "13802871832,"+
                        "13728617710,"+
                        "13823731693,"+
                        "18588265103,"+
                        "18819481056,"+
                        "18601057442,"+
                        "13302269373,"+
                        "13621656327,"+
                        "13822295905,"+
                        "13763397323,"+
                        "15138958946,"+
                        "18682088700,"+
                        "13990522221,"+
                        "13533300527,"+
                        "13488781430,"+
                        "13728619745,"+
                        "18938898303,"+
                        "15986603496,"+
                        "13723744006,"+
                        "13822231086,"+
                        "13828088102,"+
                        "18688989863,"+
                        "13632729016,"+
                        "18665399510,"+
                        "18402049506,"+
                        "18676721671,"+
                        "13632333972,"+
                        "13794601736,"+
                        "18875995992,"+
                        "17611270950,"+
                        "13288383063,"
        );


//        测试用
//        request.setPhoneNumbers("17611270950");
        //必填:短信签名-可在短信控制台中找到
        request.setSignName("新学说");
        //必填:短信模板-可在短信控制台中找到

        request.setTemplateCode("SMS_146610636");

        //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
        request.setTemplateParam("{\"name\":\"Tom\", \"code\":\"123\"}");

        //选填-上行短信扩展码(无特殊需求用户请忽略此字段)
        //request.setSmsUpExtendCode("90997");

        //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
        request.setOutId("yourOutId");

        //hint 此处可能会抛出异常，注意catch
        SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);

        return sendSmsResponse;
    }

//查询服务
    public static QuerySendDetailsResponse querySendDetails(String bizId) throws ClientException {

        //可自助调整超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");

        //初始化acsClient,暂不支持region化
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        IAcsClient acsClient = new DefaultAcsClient(profile);

        //组装请求对象
        QuerySendDetailsRequest request = new QuerySendDetailsRequest();
        //必填-号码
        //
        request.setPhoneNumber("18310081396,18813150763");
        //可选-流水号
        request.setBizId(bizId);
        //必填-发送日期 支持30天内记录查询，格式yyyyMMdd
        SimpleDateFormat ft = new SimpleDateFormat("20180510");
        request.setSendDate(ft.format(new Date()));
        //必填-页大小
        request.setPageSize(10L);
        //必填-当前页码从1开始计数
        request.setCurrentPage(1L);

        //hint 此处可能会抛出异常，注意catch
        QuerySendDetailsResponse querySendDetailsResponse = acsClient.getAcsResponse(request);

        return querySendDetailsResponse;
    }

    public static void main(String[] args) throws ClientException, InterruptedException {

        //发短信
        SendSmsResponse response = sendSms();
        System.out.println("短信接口返回的数据-------");
        System.out.println("Code=" + response.getCode());
        System.out.print("Message=" + response.getMessage());
        System.out.print("RequestId=" + response.getRequestId());
        System.out.println("BizId=" + response.getBizId());

        Thread.sleep(3000L);
        System.out.println("短信发送成功！");
        //查明细
        if(response.getCode() != null && response.getCode().equals("OK")) {
            QuerySendDetailsResponse querySendDetailsResponse = querySendDetails(response.getBizId());
            System.out.println("短信明细查询接口返回数据----------------");
            System.out.println("Code=" + querySendDetailsResponse.getCode());
            System.out.println("Message=" + querySendDetailsResponse.getMessage());
            int i = 0;
            for(QuerySendDetailsResponse.SmsSendDetailDTO smsSendDetailDTO : querySendDetailsResponse.getSmsSendDetailDTOs())
            {
                System.out.println("SmsSendDetailDTO["+i+"]:");
                System.out.println("Content=" + smsSendDetailDTO.getContent());
                System.out.println("ErrCode=" + smsSendDetailDTO.getErrCode());
                System.out.println("OutId=" + smsSendDetailDTO.getOutId());
                System.out.println("PhoneNum=" + smsSendDetailDTO.getPhoneNum());
                System.out.println("ReceiveDate=" + smsSendDetailDTO.getReceiveDate());
                System.out.println("SendDate=" + smsSendDetailDTO.getSendDate());
                System.out.println("SendStatus=" + smsSendDetailDTO.getSendStatus());
                System.out.println("Template=" + smsSendDetailDTO.getTemplateCode());
            }
            System.out.println("TotalCount=" + querySendDetailsResponse.getTotalCount());
            System.out.println("RequestId=" + querySendDetailsResponse.getRequestId());
        }

    }


}
